import smtplib
from email.header import Header
from email.mime.text import MIMEText

from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TSocket, TTransport

from message.api import MessageService

sender = 'imoocd@163.com'
authCode = 'aA111111'


class MessageServiceHandler:
    def sendMobileMessage(self, mobile, message):
        print("sendMobileMessage,mobile:" + mobile + ",message:" + message)
        return True

    def sendEmailMessage(self, email, message):
        print("sendEmailMessage,email:" + email + ",message:" + message)
        messageObj = MIMEText(message, "plain", "uft-8")
        messageObj['from'] = sender
        messageObj['To'] = email
        messageObj['Subject'] = Header('慕课网学习邮件', 'utf-8')
        try:
            smtpObj = smtplib.SMTP('smpt.163.com')
            smtpObj.login(sender,authCode)
            smtpObj.sendmail(sender,[email],messageObj.as_string())
            print('send mail success')
            return True
        except smtplib.SMTPException as ex:
            print('send mail fail')
            print(ex)
            return False

if __name__ == '__main__':
    handler = MessageServiceHandler()
    processor = MessageService.Processor(handler)
    transport = TSocket.TServerSocket(None, "9090")
    tfactory = TTransport.TFramedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
    print("python thrift server start")
    server.serve()
    print("python thrift server exit")
